<%@ WebHandler Language="C#" Class="CheckPorts" %>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;

public class CheckPorts : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string stVal = "";
        string sqlCommand = "";
        int nSuperAdmin = 0;
        if (context.Session["superadmin"] != null) nSuperAdmin = int.Parse(context.Session["superadmin"].ToString());
        if (context.Request.QueryString["subjectType"] != null)
        {
            sqlCommand = "Select FileNumber From tbl_cost_accounting WHERE IdProvince=" + context.Session["UserProvince"].ToString();
            if (context.Request.QueryString["subjectType"].ToString() == "Carrier")
                sqlCommand += " AND Agent='" + context.Request.QueryString["SubjectName"].ToString() + "'";
            else if (context.Request.QueryString["subjectType"].ToString() == "Customer")
                sqlCommand += " AND Customer='" + context.Request.QueryString["SubjectName"].ToString() + "'";
            //if (nSuperAdmin < 7)
            //    sqlCommand += " And UserID in(Select ID from tbl_adminusers where IdProvince=" + context.Session["UserProvince"].ToString() + ") ";        
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, null, null);

            if (ds.Tables[0].Rows.Count > 0)
            {

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (stVal.Length > 0)
                        stVal += "," + dr["FileNumber"].ToString().Split(',')[0].ToString();
                    else
                        stVal = dr["FileNumber"].ToString().Split(',')[0].ToString();
                }

            }
        }
        else
        {
            sqlCommand = "Select FileNumber,MBLNo,HBLNo,TotalBuyInvoice as Total_TTNCC,(TotalComAgent -TotalTaxAgent) as Total_TTCOMNCC,(TotalComCustomer- TotalTaxCustomer) as Total_TTCOMKH " +
            ",SUM(tbl_cost_accounting_detail.Amount) AS Amount"+
            " From tbl_total_cost_accounting,tbl_cost_accounting tbl_cost_accounting LEFT JOIN `tbl_cost_accounting_detail` ON  tbl_cost_accounting.id = tbl_cost_accounting_detail.CostAccountingID "+
            " WHERE tbl_cost_accounting.ID=tbl_total_cost_accounting.CostAccountingID AND FileNumber='" + context.Request.QueryString["FileNumber"].ToString() + "' Group By  tbl_cost_accounting.ID";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, null, null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                double dTotalCost = 0;
                if (context.Request.QueryString["subjectTypeValue"].ToString() == "3")
                    dTotalCost = double.Parse(ds.Tables[0].Rows[0]["Total_TTNCC"].ToString());
                else if (context.Request.QueryString["subjectTypeValue"].ToString() == "4")
                    dTotalCost = double.Parse(ds.Tables[0].Rows[0]["Total_TTCOMNCC"].ToString());
                else if (context.Request.QueryString["subjectTypeValue"].ToString() == "5")
                    dTotalCost = double.Parse(ds.Tables[0].Rows[0]["Total_TTCOMKH"].ToString());
                stVal = "{" +
                "\"MBL\":" +
                "\"" + ds.Tables[0].Rows[0]["MBLNo"].ToString() + "\"," +
                "\"HBL\":" +
                "\"" + ds.Tables[0].Rows[0]["HBLNo"].ToString() + "\"," +
                "\"Amount\":" +
                "\"" + ds.Tables[0].Rows[0]["Amount"].ToString() + "\"," +
                "\"TotalCost\":" +
                "\"" + dTotalCost.ToString() + "\"" +
                "}";
            }
        }
        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(0));
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Write(stVal);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}